package com.tyg.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tyg.entity.RoleInfo;
import com.tyg.entity.UserInfo;
import com.tyg.mapper.RoleInfoMapper;
import com.tyg.mapper.UserInfoMapper;
import com.tyg.service.RoleInfoService;
import com.tyg.service.UserInfoService;
import com.tyg.util.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author zhangjingnan
 * @since 2022-10-22
 */
@RestController
@RequestMapping("/role-info")
@CrossOrigin
@Slf4j
public class RoleInfoController {

    @Autowired
    RoleInfoMapper roleInfoMapper;

    @Autowired
    RoleInfoService roleService;

    @Autowired
    UserInfoMapper userMapper;

    @Autowired
    UserInfoService userInfoService;

    //查询所有角色
    @PostMapping("/getALLRole")
    public ResultCode getALLRole(){
        QueryWrapper<RoleInfo> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("delFlag",0);
//        List<RoleInfo> roleList = roleInfoMapper.selectList(queryWrapper);
        List<RoleInfo> roleList =roleService.selectList(queryWrapper);
        return ResultCode.success(roleList);
    }

    //查询所有角色信息
    @PostMapping("/getRoleList")
    public ResultCode getRoleList(String roleName, @RequestParam(defaultValue = "1" ,required = true)int pageIndex, int pageSize) {
        System.out.println("角色名称："+roleName);
        System.out.println("当前页数："+pageIndex);
        System.out.println("当前显示页数："+pageSize);
        PageHelper.startPage(pageIndex, pageSize);
        QueryWrapper<RoleInfo> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("delFlag",0);
        queryWrapper.like("roleName",roleName);
//        List<RoleInfo> roleList = roleInfoMapper.selectList(queryWrapper);
        List<RoleInfo> roleList =roleService.selectList(queryWrapper);
        PageInfo<RoleInfo> page = new PageInfo<RoleInfo>(roleList);
        return ResultCode.success(page);
    }

    //添加角色
    @PostMapping("/addRole")
    public ResultCode addRole(@RequestBody RoleInfo role) {
        System.out.println(role);
        try {//需要service业务逻辑层来判断
            boolean result=roleService.addRole(role);
            if(result) {
                return ResultCode.success();
            }else {
                return ResultCode.error(900, "该角色已存在,不能重复添加");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultCode.error(500, "添加失败");
        }
    }

    @PostMapping("/getRoleById")
    public ResultCode getRoleById(int roleId) {
//        RoleInfo role = roleInfoMapper.selectById(roleId);
        RoleInfo role =roleService.selectById(roleId);
        return ResultCode.success(role);
    }

    //修改角色
    @PostMapping("/updateRole")
    public ResultCode updateRole(@RequestBody  RoleInfo role) {
        System.out.println(role);
        try {//需要service业务逻辑层来判断
            boolean result=roleService.updateRole(role);
            if(result) {
                return ResultCode.success();
            }else {
                return ResultCode.error(900, "该角色已存在,不能重复修改");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultCode.error(500, "修改失败");
        }
    }

    //删除
    @PostMapping("/delRoleById")
    public ResultCode delRoleById(int roleId,int userRoleId) {
        System.out.println("userRoleId="+userRoleId);
        System.out.println("roleId="+roleId);
        try {
            if(roleId!=userRoleId) {
                RoleInfo role=new RoleInfo();
                role.setDelFlag(1);
                role.setRoleId(roleId);
//                roleInfoMapper.updateById(role);
                roleService.updateById(role);
                UserInfo user=new UserInfo();
                user.setRoleId(roleId);
                user.setDelFlag(1);
//                userMapper.updateUserRole(user);
                userInfoService.updateUserRole(user);
            return ResultCode.success();
            }else {
                return ResultCode.error(900, "该角色正在使用,不能删除");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultCode.error(500, "删除失败！");
        }
    }



}

